package system;

import system.Grade;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

         ArrayList<Student> list=new ArrayList<>();

        while(true){
            System.out.println("***欢迎来到野鸡大学学生管理系统***");
            System.out.println("********************************");
            System.out.println("         1 显示所有学生基本信息");
            System.out.println("         2 显示所有学生成绩信息");
            System.out.println("         3 录入学生信息");
            System.out.println("         4 录入学生成绩");
            System.out.println("         5 修改学生信息");
            System.out.println("         6 删除学生档案");
            System.out.println("         7 查询成绩");
            System.out.println("         8 退出系统");
            System.out.println("********************************");
            System.out.println("请输入你的操作：");
            int op=new Scanner(System.in).nextInt();
            boolean exit=false;
            switch (op){
                case 1:method1(list);break;
                case 2:method2(list);break;
                case 3:add(list);break;
                case 4:recordScore(list);break;
                case 5:changeMessage(list);break;
                case 6:remove(list);break;
                case 7:query(list);break;
                case 8:exit=true;break;
            }
            if(exit)break;
        }
    }

    public static void method1(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("不好意思，你未曾录入过学生信息");
            return;
        }
        System.out.print("学号         姓名");
        for (int i = 0; i < SpecialVariable.SpaceNum-3; i++) {
            System.out.print(" ");
        }
        System.out.println("年龄  出生日期  地址");
        for(int i=0;i<list.size();i++){
            Student student=list.get(i);
            System.out.print(student.getNum()+"  "+student.getName());
            for (int j = 0; j < SpecialVariable.SpaceNum-student.getName().length() ; j++) {
                System.out.print(" ");
            }
            System.out.println(student.getAge()+"    "+student.getDate()+"  "+student.getAddress());
        }
        System.out.println("所有学生信息如上，总共"+list.size()+"人");
    }
    public static void method2(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println("抱歉，未录入学生信息");
            return;
        }

        for (int i = 0; i <list.size() ; i++) {
            int sum=0;
            Student student=list.get(i);
            System.out.print("学号： "+student.getNum()+"   姓名： "+student.getName()+"  ");
            int size=student.getGrade().size();
            if(size==0){
                System.out.println("该学生没有成绩");
                continue;
            }
            for (int j = 0; j < size; j++) {
                Grade grade= student.getGrade().get(j);
                sum+=grade.getScore();
                System.out.print(grade.getSubject()+":"+grade.getScore()+"分");
                 System.out.print("   ");
            }
            System.out.println("总分： "+sum+"分   "+"平均分： "+sum*1.0/size+"分");
        }
    }
    public  static void add(ArrayList<Student>list){

        String num,name,address,date;
        int age;
        num= SpecialVariable.str+setStudentNumber(SpecialVariable.stuNum++);
        System.out.println("开始录入学生基本信息");
        Scanner sc=new Scanner(System.in);
        System.out.println("学生姓名：");
        name=sc.next();
        System.out.println("学生年龄：");
        age=sc.nextInt();
        while(age<=0){
            System.out.println("输入的年龄不能为负数，请重新输入");
            age=sc.nextInt();
        }
        System.out.println("学生出生日期：(格式：090123)");
        date=sc.next();
        while(!check(date)){
            System.out.println("输入的学生出生日期不合要求，请重新输入");
            date=sc.next();
        }
        System.out.println("学生住址");
        address=sc.next();
        ArrayList<Grade>grades=new ArrayList<>();
        Student student=new Student(num,name,age,address,grades,date);
        list.add(student);
        System.out.println("录入成功");

    }
    private static boolean check(String s){
        /*
        检查输入的学生出生日期是否按要求录入
         */

        if(s.length()!=6||
                ((s.charAt(2)-'0')*10+(s.charAt(3)-'0'))<=0||((s.charAt(2)-'0')*10+(s.charAt(3)-'0'))>12||
                ((s.charAt(4)-'0')*10+(s.charAt(5)-'0'))<=0||((s.charAt(4)-'0')*10+(s.charAt(5)-'0'))>31){
            return false;
        }
        return true;
    }
    public static String setStudentNumber(int num){
        int len;
        if(num==0)
        {
            len=1;
        }
       else{
            len=(int)Math.log10((double)num)+1;
        }
//        System.out.println(len);
        String str="";
        for (int i = 0; i < (4 - len); i++) {
            str=str+0;
        }
        str=str+num;
        return str;
    }
    public static void recordScore(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println("抱歉，你未曾录入学生信息，无法进行成绩录入操作");
            return;
        }
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入你将录入的成绩所属科目的数量：");
        int num=sc.nextInt();
        for (int i = 0; i < num; i++) {
            System.out.println("输入录入成绩所属科目");
            String subject=sc.next();
            for (int i1 = 0; i1 < list.size(); i1++) {
                Student student=list.get(i1);
                System.out.println(student.getNum() +"  "+student.getName()+"  "+subject);
                System.out.println(subject+"成绩为：");
                int score=sc.nextInt();
                Grade grade=new Grade(subject,score);
                student.getGrades().add(grade);
            }
        }
        System.out.println("录入成功");

    }
    public static  void  remove(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println("抱歉，你未曾录入学生信息，无法进行移除操作");
            return;
        }
        System.out.println("请输入你要移除学生的学号： ");
        String str=new Scanner(System.in).next();
        for (int i = 0; i < list.size(); i++) {
            Student student=list.get(i);
            if(student.getNum().equals(str)){
                list.remove(student);
                System.out.println("学号为"+str+"的学生已移除");
                return;
            }
        }
        System.out.println("该学生不存在");
    }
    public static void changeMessage(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println();//未完成
        }
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你将要删除的学生的学号：");
        String str=sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student student=list.get(i);
            if(student.getNum().equals(str)){
                System.out.println("请选择你要修改的信息：1表示姓名，2表示年龄，3表示出生日期，4表示学生住址");
                int op=sc.nextInt();
                System.out.println("输入你将修改的信息");
                switch (op){
                    case 1:student.setName(sc.next());break;
                    case 2:student.setAge(sc.nextInt());break;
                    case 3:
                        System.out.println("格式(090123)");
                        String date=sc.next();
                        while(!check(date)){
                            System.out.println("输入的学生出生日期不合要求，请重新输入");
                            date=sc.next();
                        }
                        student.setDate(date);break;
                    case 4:student.setDate(sc.next());break;
                }
                System.out.println("修改成功");
                return ;
            }

        }
        System.out.println("该学生不存在");
    }
    public static void query(ArrayList<Student>list){
        System.out.println("请输入你将要查询成绩的学生的学号：");
        String str=new Scanner(System.in).next();
        for (int i = 0; i < list.size(); i++) {
            Student student=list.get(i);
            if(student.getNum().equals(str)){
                System.out.print("学号： "+student.getNum()+"  姓名： "+student.getName());
                if(student.getGrades().size()==0){
                    System.out.println("该学生没有成绩");
                }
                for (int i1 = 0; i1 < student.getGrades().size(); i1++) {
                    Grade grade=student.getGrades().get(i1);
                    System.out.println("  "+grade.getSubject()+": "+grade.getScore()+"分");
                }
                System.out.println("查找完成");
                return ;
            }
        }
        System.out.println("未找到该学生，请检查是否输入错误,<<如果无误,请联系相关负责人");
    }
}
